//
//  PywCommonRequestApi.m
//  PywCommonSDKDemo
//
//  Created by Ampaw on 2017/8/11.
//  Copyright © 2017年 Ampaw. All rights reserved.
//

#import "PywCommonRequestApi.h"
#import "PywCommonDeviceInfo.h"
#import "PywCommonPlatformInfo.h"

@implementation PywCommonRequestApi

/**
 请求 聚合SDK初始化接口
 
 @param params 请求参数
 @param completion 完成回调
 */
+ (void)requestInitWithParams:(NSDictionary *)params Completion:(CommonSDKCompletion)completion
{
    //添加网络监听，每次网络变化都会进行初始化
    [[PywCommonDeviceInfo sharedCommonDeviceInfo] reachabilityStartMonitoringAndStatusChangeBlock:^(AFNetworkReachabilityStatus status)
     {
         if([PywCommonPlatformInfo sharedCommonPlatformInfo].isInitSuccess == false)
         {
             NSString *gamekey = [PywCommonPlatformInfo sharedCommonPlatformInfo].gamekey;
             NSString *urlString = [NSString stringWithFormat:@"%@%@.txt",baseUrl,gamekey];
             [PywCommonRequestHelper requestApiForSdkUrl:urlString success:^(NSDictionary *dict) {
                 // 存储api域名地址
                 [PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_common_apiurl = [dict objectForKey:@"base_url"];
                 [PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_channel_apiurl = [dict objectForKey:@"pyw_channel_url"];
                 [PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_pay_apiurl = [dict objectForKey:@"pay_url"];
                 
                 if ([PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_common_apiurl.length > 0) {
                     [PywCommonPlatformInfo sharedCommonPlatformInfo].request_apiurl = [PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_common_apiurl;
                 }else{
                     [PywCommonPlatformInfo sharedCommonPlatformInfo].request_apiurl = RequestBaseURL;
                 }
                 
                 // 请求聚合init
                 [PywCommonRequestHelper requestWithParams:params
                                            requestBaseUrl:[PywCommonPlatformInfo sharedCommonPlatformInfo].request_apiurl
                                             requestMethod:api_init
                                                  complete:completion];
             } failer:^(NSError *error) {
                 [PywCommonPlatformInfo sharedCommonPlatformInfo].request_apiurl = RequestBaseURL;
                 // 请求聚合init
                 [PywCommonRequestHelper requestWithParams:params
                                            requestBaseUrl:RequestBaseURL
                                             requestMethod:api_init
                                                  complete:completion];
             }];
         }
     }];
}

/**
 请求 聚合SDK登录接口
 
 @param params 请求参数
 @param completion 完成回调
 */
+ (void)requestLoginWithParams:(NSDictionary *)params Completion:(CommonSDKCompletion)completion
{
    [PywCommonRequestHelper requestWithParams:params
                               requestBaseUrl:[PywCommonPlatformInfo sharedCommonPlatformInfo].request_apiurl
                                requestMethod:api_login
                                     complete:completion];
}

/**
 请求 上报游戏角色
 
 @param params 请求参数
 @param completion 完成回调
 */
+ (void)requestUploadRoleInfoWithParams:(NSDictionary *)params Completion:(CommonSDKCompletion)completion
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *upload_url = UploadDataURL;
        if ([PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_channel_apiurl.length > 0) {
            upload_url =[PywCommonPlatformInfo sharedCommonPlatformInfo].pyw_channel_apiurl;
        }
        [PywCommonRequestHelper requestWithParams:params
                                   requestBaseUrl:upload_url
                                    requestMethod:api_dataCollect
                                         complete:completion];
    });
}

/**
 请求 聚合SDK创建订单接口
 
 @param params 请求参数
 @param completion 完成回调
 */
+ (void)requestCreatOrderWithParams:(NSDictionary *)params Completion:(CommonSDKCompletion)completion
{
    [PywCommonRequestHelper requestWithParams:params
                               requestBaseUrl:[PywCommonPlatformInfo sharedCommonPlatformInfo].request_apiurl
                                requestMethod:api_createOrder
                                     complete:completion];
}

/**
 请求 聚合SDK充值支付接口
 
 @param params 请求参数
 @param completion 完成回调
 */
+ (void)requestStartPayWithParams:(NSDictionary *)params Completion:(CommonSDKCompletion)completion
{
    [PywCommonRequestHelper requestWithParams:params
                               requestBaseUrl:[PywCommonPlatformInfo sharedCommonPlatformInfo].request_apiurl
                                requestMethod:api_pay
                                     complete:completion];
}


@end
